﻿namespace GSSoftSL
{
    using System;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// <see cref="ChildWindow"/> class that displays errors to the user.
    /// </summary>
    public partial class ConfirmWindow : ChildWindow
    {
        public static EnumDialogButton LastButtonClick { get; set; }
        private Action _callBack;

        /// <summary>
        /// Creates a new <see cref="ConfrimWindow"/> instance.
        /// </summary>
        /// <param name="message">The error message to display.</param>
        protected ConfirmWindow(string message, Action CallBack)
        {
            InitializeComponent();
            //IntroductoryText.Text = message;
            ErrorTextBox.Text = message;
            _callBack = CallBack;
        }

        #region Factory Methods
        /// <summary>
        /// All other factory methods will result in a call to this one.
        /// </summary>
        /// <param name="message">The message to display</param>
        public static void CreateNew(string message, Action callBack)
        {
            ConfirmWindow window = new ConfirmWindow(message, callBack);
            window.Show();
        }
        #endregion

        #region Factory Helpers
        /// <summary>
        /// Returns whether running with a debugger attached or with the server hosted on localhost.
        /// </summary>
        private static bool IsRunningUnderDebugOrLocalhost
        {
            get
            {
                if (Debugger.IsAttached)
                {
                    return true;
                }
                else
                {
                    string hostUrl = Application.Current.Host.Source.Host;
                    return hostUrl.Contains("::1") || hostUrl.Contains("localhost") || hostUrl.Contains("127.0.0.1");
                }
            }
        }

        /// <summary>
        /// Creates a user-friendly message given an Exception. 
        /// Currently this method returns the Exception.Message value. 
        /// You can modify this method to treat certain Exception classes differently.
        /// </summary>
        /// <param name="e">The exception to convert.</param>
        private static string ConvertExceptionToMessage(Exception e)
        {
            return e.Message;
        }
        #endregion

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //LastButtonClick = EnumDialogButton.OK;
            this.DialogResult = true;
            if (_callBack != null)
                _callBack();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            //LastButtonClick = EnumDialogButton.Cancel;
            this.DialogResult = true;
        }
    }

    public enum EnumDialogButton
    {
        OK,
        Cancel
    }
}